<template>
  <el-dialog :title="title===0?'新增目录':'修改目录'" :visible="show" @close="close" width="400px">
    <el-form :model="form" ref="ruleForm" :rules="rules">
      <el-form-item label="所属学科" label-width="80px" prop="subjectID">
        <el-select
          v-model="form.subjectID"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in subjects"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" label-width="80px" prop="directoryName">
        <el-input
          v-model.trim="form.directoryName"
          autocomplete="off"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="onSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入新增目录接口 修改目录接口 目录详情
import { addDirectory, modifyDirectory, getDetail } from '@/api/hmmm/directorys'
export default {
  name: 'DirectoryAdd',
  data () {
    return {
      // input表单数据
      form: {
        subjectName: '',
        directoryName: '',
        subjectID: '',
        id: null
      },
      // 校验规则
      rules: {
        subjectID: [{ required: true, message: '请选择所属学科' }],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    // 显示
    show: {
      type: Boolean,
      required: true,
      default: false
    },
    // 数据
    subjects: {
      type: Array,
      required: true
    },
    // 标题名
    title: {
      type: Number
    }
  },
  methods: {
    // 关闭事件
    close () {
      // 更新弹出显示状态
      this.$emit('update:show', false)
      // 清空弹层数据
      this.$refs.ruleForm.resetFields()
    },
    // 提交事件
    async onSubmit () {
      // console.log(this.form)
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.title === 0) {
            // 发ajax新增目录
            await addDirectory(this.form)
          } else {
            // 发请求修改
            await modifyDirectory(this.form)
          }
          // 关闭弹层
          this.close()
          // 重新拿数据渲染
          this.$emit('getDirectorytList')
          // console.log(111)
        }
      })
    },
    // 目录详情
    async getDetail (obj) {
      // 获取目录详情
      const { data } = await getDetail(obj)
      this.form = data
    }
  }
}
</script>

<style scoped lang='less'></style>
